local anguo = fk.CreateSkill {
  name = "tyj_heg__anguo",
  tags = {Skill.Compulsory}
}
local H = require "packages.ol_hegemony.util"
local U = require "packages.utility.utility"
Fk:loadTranslationTable{
      ["tyj_heg__anguo"]="安国",
      [":tyj_heg__anguo"]="锁定技，与你势力相同的角色视为装备着你装备区内的武器/防具牌，若其已装备则改为【杀】的使用次数/手牌上限+1。",
      ["$tyj_heg__anguo1"] = "感文台知遇，自当鞠躬尽瘁，扶其身后之业。",
      ["$tyj_heg__anguo2"] = "孙氏为危难之际，吾当尽力辅之！",
      ["$tyj_heg__anguo3"] = "安国定邦，克成东南一统！",
      ["~tyj_heg__zhuzhi"]="臣辅孙氏三代之业，今年近古稀，死而无憾。",
    }
---@param player ServerPlayer
    local function doAnJiangToPlayer(player)
      local room = player.room
      local tocheck = room.alive_players
      for _, skn in ipairs(player:getTableMark(anguo.name)) do
        room:handleAddLoseSkills(player, "-"..skn[1], nil,false,true)
      end
        local mark = {}
      for _, p in ipairs(tocheck) do
        if p~=player and p:hasShownSkill(anguo.name) and H.compareKingdomWith(p, player) then
        if #player:getAvailableEquipSlots(Card.SubtypeArmor) > 0 and p:getEquipment(Card.SubtypeArmor)~=nil and player:getEquipment(Card.SubtypeArmor)==nil then
          local c = Fk:getCardById(p:getEquipment(Card.SubtypeArmor))
          if c.equip_skill and not player:hasSkill(c.equip_skill,true,true) then
            room:handleAddLoseSkills(player, c.equip_skill.name, nil,false,true)
            table.insertIfNeed(mark,{c.equip_skill.name})
          end
        end
        if #player:getAvailableEquipSlots(Card.SubtypeWeapon) > 0 and p:getEquipment(Card.SubtypeWeapon)~=nil and player:getEquipment(Card.SubtypeWeapon)==nil then
          local c = Fk:getCardById(p:getEquipment(Card.SubtypeWeapon))
          if c.equip_skill and not player:hasSkill(c.equip_skill,true,true) then
            room:handleAddLoseSkills(player, c.equip_skill.name, nil,false,true)
            table.insertIfNeed(mark,{c.equip_skill.name,c.attack_range})
          end
        end
      end
      end
      if #mark >0 then
        room:setPlayerMark(player,anguo.name,mark)
      end
    end
    ---@param room Room
    local function doAnJiang(room)
      local tocheck = room.alive_players
      for _, player in ipairs(tocheck) do
        for _, skn in ipairs(player:getTableMark(anguo.name)) do
          room:handleAddLoseSkills(player, "-"..skn[1], nil,false,true)
        end
          local mark = {}
        for _, p in ipairs(tocheck) do
          if p~=player and p:hasShownSkill(anguo.name) and H.compareKingdomWith(p, player) then
          if #player:getAvailableEquipSlots(Card.SubtypeArmor) > 0 and p:getEquipment(Card.SubtypeArmor)~=nil and player:getEquipment(Card.SubtypeArmor)==nil then
            local c = Fk:getCardById(p:getEquipment(Card.SubtypeArmor))
            if c.equip_skill and not player:hasSkill(c.equip_skill,true,true) then
              room:handleAddLoseSkills(player, c.equip_skill.name, nil,false,true)
              table.insertIfNeed(mark,{c.equip_skill.name})
            end
          end
          if #player:getAvailableEquipSlots(Card.SubtypeWeapon) > 0  and p:getEquipment(Card.SubtypeWeapon)~=nil and player:getEquipment(Card.SubtypeWeapon)==nil then
            local c = Fk:getCardById(p:getEquipment(Card.SubtypeWeapon))
            if c.equip_skill and not player:hasSkill(c.equip_skill,true,true) then
              room:handleAddLoseSkills(player, c.equip_skill.name, nil,false,true)
              table.insertIfNeed(mark,{c.equip_skill.name,c.attack_range})
            end
          end
        end
        end
        if #mark >0 then
          room:setPlayerMark(player,anguo.name,mark)
        end
      end
    end

    anguo:addEffect(fk.SkillEffect,{
        can_refresh = function (self, event, target, player, data)
        return target == player
      end,
      on_refresh = function (self, event, target, player, data)
        local room = player.room
        if #table.filter(player:getTableMark(anguo.name),function (tos)
              return data.skill:getSkeleton().name == tos[1]
            end)>0 then
              for _, p in ipairs(room.alive_players) do 
                if p~=player and p:hasShownSkill(anguo.name) and H.compareKingdomWith(p, player) then
              p:broadcastSkillInvoke(anguo.name)
          room:notifySkillInvoked(p, anguo.name)
                end
              end
            end
      end,
    })
    anguo:addEffect(fk.GeneralRevealed,{
        can_refresh = function (self, event, target, player, data)
        return target == player
      end,
      on_refresh = function (self, event, target, player, data)
        local room = player.room
        if player:hasSkill(anguo.name,true,true) then
            doAnJiang(room)
          else
            doAnJiangToPlayer(player)
          end
      end,
    })
    anguo:addEffect(fk.GeneralRevealed,{
        can_refresh = function (self, event, target, player, data)
        return target == player
      end,
      on_refresh = function (self, event, target, player, data)
        local room = player.room
        if player:hasSkill(anguo.name,true,true) then
            doAnJiang(room)
          else
            doAnJiangToPlayer(player)
          end
      end,
    })
    anguo:addEffect(fk.Deathed,{
        can_refresh = function (self, event, target, player, data)
        return target == player
      end,
      on_refresh = function (self, event, target, player, data)
        local room = player.room
        doAnJiang(room)
      end,
    })
    anguo:addEffect(fk.AfterCardsMove,{
        can_refresh = function (self, event, target, player, data)
        return true
      end,
      on_refresh = function (self, event, target, player, data)
        local room = player.room
          local gonnaanjiang = false
          for _, move in ipairs(data) do
            if move.from == player  then
              for _, info in ipairs(move.moveInfo) do
                if info.fromArea == Card.PlayerEquip then
                  if (Fk:getCardById(info.cardId).sub_type == Card.SubtypeWeapon) or (Fk:getCardById(info.cardId).sub_type == Card.SubtypeArmor) then
                    gonnaanjiang = true
                  end
                end
              end
            elseif move.to == player and move.toArea == Card.PlayerEquip then
              for _, info in ipairs(move.moveInfo) do
                if (Fk:getCardById(info.cardId).sub_type == Card.SubtypeWeapon) or (Fk:getCardById(info.cardId).sub_type == Card.SubtypeArmor) then
                  gonnaanjiang = true
                end
              end
            end
          end
          if gonnaanjiang == true then
            doAnJiang(room)
          end
      end,
    })
    anguo:addEffect("maxcards",{
      correct_func = function(self, player)
        local room = Fk:currentRoom()
        if player.phase ~= Player.NotActive and #player:getAvailableEquipSlots(Card.SubtypeArmor) > 0 and player:getEquipment(Card.SubtypeArmor)~=nil then
        local n = 0
        n= n+#table.filter(room.alive_players,function(p) return p:hasShownSkill("tyj_heg__anguo") and H.compareKingdomWith(p, player) and
          p:getEquipment(Card.SubtypeArmor)~=nil and player:getEquipment(Card.SubtypeArmor)~=nil
           end )
        if n>0 then
        return n
      end
    end
      end,
    })
    anguo:addEffect("targetmod",{
residue_func = function(self, player, skill, scope,card,to)
        local room = Fk:currentRoom()
        local n = 0
        if #player:getAvailableEquipSlots(Card.SubtypeWeapon) > 0 and player:getEquipment(Card.SubtypeWeapon)~=nil then
        n= n+#table.filter(room.alive_players,function(p) return p:hasShownSkill("tyj_heg__anguo") and H.compareKingdomWith(p, player) and
          p:getEquipment(Card.SubtypeWeapon)~=nil
           end )
        if n>0 then
        return n
      end
    end
      end,
    })
    anguo:addEffect("atkrange",{
      fixed_func = function (self, from)
        local player = from
        local mark = player:getTableMark("tyj_heg__anguo")
        local range = 0
        for _, wn in ipairs(mark) do
          if #wn>1 and range<wn[2] then
            range = wn[2]
          end
        end
        if range~=0 then
          return range
        end
      end,
    })
    anguo:addAcquireEffect(function(self, player, is_start)
    local room = player.room
    doAnJiang(room)
end)
    anguo:addLoseEffect(function(self, player, is_start)
    local room = player.room
    doAnJiang(room)
end)
anguo:addEffect(fk.AfterPropertyChange, {
  can_refresh = function (self, event, target, player, data)
    if target == player and (data.results["kingdomChange"]) then
      return true
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if player:hasSkill(anguo.name,true,true) then
            doAnJiang(room)
          else
            doAnJiangToPlayer(player)
          end
  end,
})
    return anguo